class Solution {
public:
    int maxProfit(vector<int> &prices) {
        int ret=0, index=-1;
        for (int i=0;i<prices.size();i++)
            if (index==-1)
                index=i;
            else
                if (prices[i-1]>prices[i]) {
                    ret+=prices[i-1]-prices[index];
                    index=i;
                }
        if (index!=-1) ret+=prices[prices.size()-1]-prices[index];
        return ret;
    }
};
